package com.yanqu.road.server.cmd.activity.newyear2023.firework;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.newyear2023.config.FireWorkConfig;
import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FireWorkProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.newyear2023.FireWorkModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.logic.bag.BaseUseGoods;
import com.yanqu.road.server.manger.activity.newyear2023.FireWorkMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_FIREWORK_FIRE, desc = "放烟花")
public class FireWorkFireCmd extends AbstractCommand {

    private int activityId;
    private int itemId;
    private int count;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        FireWorkProto.FireWorkFireReqMsg reqMsg = FireWorkProto.FireWorkFireReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        itemId = reqMsg.getItemId();
        count = reqMsg.getCount();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = FireWorkMgr.getTypeActivity(1);
        if(!FireWorkMgr.isActivityInTime(activityId) || activityInfo == null || activityInfo.getActivityId() != activityId){
            ret = GameErrorCode.E_ACTIVITY_HAD_END;
            return;
        }
        if(count > 100 || count < 1){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        BagModule bagModule = player.getModule(BagModule.class);
        UserBagItem bagItem = bagModule.getUserBagItem(itemId);
        if (bagItem == null || bagItem.getNum() <= 0) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(itemId);
        if(null == goodsInfo){
            ret = GameErrorCode.E_GOODS_NO_EXIST;
            return;
        }

        ret = player.getModule(FireWorkModule.class).fireWorkFire(activityId,itemId,count);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            FireWorkProto.FireWorkFireRespMsg.Builder respMsg = FireWorkProto.FireWorkFireRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_FIREWORK_FIRE, respMsg);
        }
    }
}
